package com.gitee.huxing.framework.utils;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description: 线程工具类
 * @ProjectName: SpringCloudLearning
 * @Package: com.huxing.framework.utils
 * @ClassName: ThreadTaskHelper
 * @Author: huxing
 * Date: 2021-05-24
 * Time: 10:58
 */
@Slf4j
@SuppressWarnings("all")
public class ThreadTaskHelper {

    private static ExecutorService executorService= Executors.newFixedThreadPool(20);

    private static Map RUNNING_REFRESH_CACHE= Maps.newConcurrentMap();

    public static Map<String,String> getRunningRefreshCache(){
        return RUNNING_REFRESH_CACHE;
    }

    public static void putRefreshCacheTask(String cacheKey){
        log.info("添加到缓存更新管理线程中: {}, {}", cacheKey, LocalDateTime.now());
        if(!hasRunningRefreshCacheTask(cacheKey)){
            RUNNING_REFRESH_CACHE.put(cacheKey,cacheKey);
        }
    }

    public static void removeRefreshCacheTask(String cacheKey){
        if(hasRunningRefreshCacheTask(cacheKey)){
            RUNNING_REFRESH_CACHE.remove(cacheKey);
        }
    }

    public static boolean hasRunningRefreshCacheTask(String cacheKey){
        return RUNNING_REFRESH_CACHE.containsKey(cacheKey);
    }

    public static void run(Runnable runnable){
        executorService.execute(runnable);
    }
}
